<?
    function getNLSystems($xml_file) {
        global $sistemas;
        //$objeto  = domxml_open_file($xml_file);
        //$raiz = $objeto->document_element();
        $objeto = new DOMDocument();
        $objeto->load($xml_file);
        $filho = $objeto->getelementsbytagname("nonlin_systems");
        if ($filho->length > 0) {
            $temp = $filho->item(0)->getelementsbytagname("nl_system");
            for($i = 0; $i < $temp->length; $i++) {
                $sistemas[$i] = $temp->item($i);
            }
            return $sistemas;
        }
        else return null;
    }

    function getDim($indice) {
        global $sistemas, $dim;
        $dimensao = $sistemas[$indice]->getelementsbytagname("dim");
        $dim = $dimensao->item(0)->nodeValue;
        return $dim;
    }

    function getMaxIter($indice) {
        global $sistemas, $mx;
        $maxiter = $sistemas[$indice]->getelementsbytagname("maxiter");
        $mx = $maxiter->item(0)->nodeValue;
        return $mx;
    }

    function getEpsilon($indice) {
        global $sistemas, $eps;
        $epsl = $sistemas[$indice]->getelementsbytagname("epsilon");
        $eps = $epsl->item(0)->nodeValue;
        return $eps;
    }

    function getIVector($indice) {
        global $sistemas, $ivector;
        $ivvv = $sistemas[$indice]->getelementsbytagname("initial_vector");
        if ($ivvv->length > 0) {
                $x = $ivvv->item(0)->getelementsbytagname("x");
                for($j = 0; $j < $x->length; $j++) {
                $ivector[$j] = $x->item($j)->nodeValue;
            }
            return $ivector;
        }
        else return null;
    }

    function getDesc($indice) {
        global $sistemas, $desc;
        $descricao = $sistemas[$indice]->getelementsbytagname("description");
        $desc = $descricao->item(0)->nodeValue;
        return $desc;
    }

    function getCom($indice) {
        global $sistemas, $com;
        $comentario = $sistemas[$indice]->getelementsbytagname("comments");
        if ($comentario->length > 0) {
            $com = $comentario->item(0)->nodeValue;
            return $com;
        }
        else return null;
    }

    function getRef($indice) {
        global $sistemas, $ref;
        $referencia = $sistemas[$indice]->getelementsbytagname("reference");
        if ($referencia->length > 0) {
            $ref = $referencia->item(0)->nodeValue;
            return $ref;
        }
        else return null;
    }

    function getSystem($indice) {
        global $sistemas, $sys;
        $nlrow = $sistemas[$indice]->getelementsbytagname("nl_row");
        if ($nlrow->length > 0) {
            for($i = 0; $i < $nlrow->length; $i++) {
                $x = $nlrow->item($i)->getelementsbytagname("x");
                for($j = 0; $j < $x->length; $j++) {
                $sys[$i][$j]["c"] = $x->item($j)->nodeValue;
                }
            }
            return $sys;
        }
        else return null;
    }

    function getExpoents($indice) {
        global $sistemas, $expt;
        $nlrow = $sistemas[$indice]->getelementsbytagname("nl_row");
        if ($nlrow->length > 0) {
            for($i = 0; $i < $nlrow->length; $i++) {
                $exp = $nlrow->item($i)->getelementsbytagname("exp");
                for($j = 0; $j < $exp->length; $j++) {
                $expt[$i][$j]["g"] = $exp->item($j)->nodeValue;
                }
            }
            return $expt;
        }
        else return null;
    }

    function createMenu() {
        global $sistemas,$msg;
        for($i = 0; $i < sizeof($sistemas); $i++) {
            echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
            echo $msg->get('XML_EXIBE_SEQNL_MSG01');
            echo getDim($i);
            echo "</u></a><p>";
        }
        return;
    }

    if (!isset($xml_file) && empty($id_bib)) $id_bib="bib_demo.xml";
    if ( (strlen(@$indice)==0) || ($indice == null) ) { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function createLinks(value) {
               document.myform.indice.value = value;
               document.myform.action = 'index.php?inc=seqnl.php&modo=exemplo';
               document.myform.submit();
            }

        </script>
        <? if (isset($id_bib)) $xml_file = $DOCUMENT_ROOT."/bibliotecas/".$id_bib; ?>
        <? verificaBib(@$xml_file); ?>
        <?  if (file_exists($xml_file)) {
                $temp = getNLSystems($xml_file);
                if (!$temp==null) {
                   echo "<p>";
                   echo "<p align=left><font face=Arial>".$msg->get('XML_EXIBE_MSG04')."</font>";
                   echo "<p>";
                   createMenu();
                } else {
                     echo $msg->get('XML_EXIBE_MSG05');
                     echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
                }
	    } else {
                 echo $msg->get('XML_EXIBE_MSG06');
                 echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
	    }
 	 ?>

        <input type="hidden" name="id_bib" value="<? echo $id_bib ?>">
        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="indice">
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG07'); ?> onClick="history.back()">
        </form>

<? } else { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function sendToCalculus() {
               document.myform.Calcular.value = 'sim';
               document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&indice=<?echo $indice?>&inc=seqnl.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? getNLSystems($xml_file); ?>
        <? $dim = getDim($indice); ?>
        <? $k = getMaxIter($indice); ?>
        <? $t = getEpsilon($indice); ?>
        <? $vtx = getIVector($indice); ?>
        <? $desc = getDesc($indice); ?>
        <? $com = getCom($indice); ?>
        <? $ref = getRef($indice); ?>
        <? $S = getSystem($indice);
           $G = getExpoents($indice);
   		   for ($i=0; $i<$dim; $i++) {
		      for ($j=0; $j<$dim; $j++){
			    $matriz[$i][$j]["coef"] = $S[$i][$j]["c"];
			    $matriz[$i][$j]["grau"] = $G[$i][$j]["g"];
		      }
		      $matriz[$i][$j]["coef"] = $S[$i][$j]["c"];
		  }
        ?>

        <? verificaBib(@$xml_file); ?>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_SEQNL_MSG02'); ?> <? echo $dim ?></font>
		<input type=hidden name=dim value="<? echo $dim ?>">
		<p>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_SEQNL_MSG03'); ?> <? echo $k ?></font>
		<input type=hidden name=k value="<? echo $k ?>">
		<p>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_SEQNL_MSG04'); ?> 10<sup>-</font><font size="2"><? echo $t ?></sup></font>
		<input type=hidden name=t value="<? echo $t ?>">
		<p>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_SEQNL_MSG05'); ?></font>
		<p>
		<? for ($i=0; $i<$dim; $i++) {	?>
	   	<?     for ($j=0; $j<$dim; $j++) {
	            $aux=$j+1;  ?>
			    <input type="text" name="matriz[<? echo $i ?>][<? echo $j ?>][coef]" value="<? echo $matriz[$i][$j]["coef"] ?>" maxlength="10" size="5">
			    <font face="Arial">X<sub><? echo ("$aux") ?></sub></font>
			    <sup><sup><sup><sup>
			    <input type="text" maxlength="2" size="2" name="matriz[<? echo $i ?>][<? echo $j ?>][grau]" value="<? echo $matriz[$i][$j]["grau"] ?>">
                </sup></sup></sup></sup>
	        	<?  if ($j<($dim-1)) echo (" + ");
	          } //end for j
	  		echo (" = ");  ?>
			    <input type="text" maxlength="10" size="5" name="matriz[<? echo $i ?>][<? echo $j ?>][coef]" value="<? echo $matriz[$i][$j]["coef"] ?>">
			<br><br>
		<? } //end for i
        ?>
		<p align="left"><font face="Arial"><? echo $msg->get('XML_EXIBE_SEQNL_MSG06'); ?><sub><font size="2">0</sub> = (</font>
		<? for ($i=0; $i<$dim; $i++) { ?>
			<input type="text" name="vtx[<? echo $i ?>]" value="<? echo $vtx[$i] ?>" maxlenght="10" size="5">
			<? if ($i!=($dim-1)) echo ("  ,  "); ?>
		<? } ?>
        <font face="Arial">)</font>
        <? if (!$com == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG08');?></b><? echo htmlentities(utf8_decode($com)) ?></font>
        <? } ?>
        <p>
        <? if (!$ref == null) { ?>
              <p align="left"><font face="Arial"><b><? echo $msg->get('XML_EXIBE_MSG09'); ?> </b><? echo htmlentities(utf8_decode($ref)) ?></font>
        <? } ?>
        <p>


        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="Calcular" >
	<input type="hidden" name="navegacao" value="sim">
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG010'); ?> onClick="sendToCalculus()"><p>
        </form>
	<?
	echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
	echo "<script language=\"javascript\" type=\"text/javascript\">\n";
	echo "function criarLinks(value) {\n";
	echo "document.exemplos.indice.value = value;\n";
	echo "document.exemplos.action = 'index.php?inc=seqnl.php&modo=exemplo'\n";
	echo "document.exemplos.submit();\n";
	echo "}\n";
	echo "</script>";
	echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
	echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
	echo "<input type=\"hidden\" name=\"indice\">";
	echo "</form>";
	echo $msg->get('Outros_Exemplos')."<br>";
	//$objeto  = domxml_open_file($xml_file);
	//$raiz = $objeto->document_element();
    $objeto = new DOMDocument();
    $objeto->load($xml_file);
	$filho = $objeto->getelementsbytagname("nonlin_systems");
	if ($filho->length > 0)
	{
		$temp = $filho->item(0)->getelementsbytagname("nl_system");
		for($i = 0; $i < $temp->length; $i++)
		{
			//$sistemas[$i] = $temp[$i];
			if ($i != 0) echo " | ";
			if ($i == $indice) echo "<b>";
			echo "<a href=\"javascript:criarLinks($i)\"><u>".($i+1)."</u></a>";
			if ($i == $indice) echo "</b>";
		}
	}
	?>

<? } ?>
